/*
 * author M.Chang
 *
 * Copyright 2008 Ph.Bonnet and M.Chang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

#include "DataSet.h"


/*****************************************************************************/

/* constructor */
DataSet::DataSet()
{
  size = 0;
  total_reward = 0;
  total_energy = 0;
  total_time = 0;
  total_transmit = 0;
  total_bandwidth = 0;
  total_neighbor_energy = 0;
  total_neighbor_time = 0;

//  allNodes = false;

}


/* destructor */
DataSet::~DataSet()
{
//  std::cout << "DataSet destructor called" << std::endl;

}

/*
void DataSet::addNode(int id)
{
  nodes.insert(id);
}

void DataSet::addNodes(std::set<int> ids)
{
  nodes.insert(ids.begin(), ids.end());
}

void DataSet::addAllNodes()
{
  allNodes = true;
}
*/

void DataSet::addFrequencySet(std::set<int> frequency_set)
{
  frequency_domain_set.insert(frequency_set.begin(), frequency_set.end());
}


void DataSet::addElement(int element, int reward, int energy, int time, int transmit, int bandwidth, int neighbor_energy, int neighbor_time)
{
  domain_set.insert(element);
  total_reward += reward;
  total_energy += energy;
  total_time += time;
  total_transmit += transmit;
  total_bandwidth += bandwidth;
  total_neighbor_energy += neighbor_energy;
  total_neighbor_time += neighbor_time;

  std::stringstream ss;
  ss << element;
  domain_vector.push_back(ss.str());

  ss.str("");
  ss << reward;
  reward_vector.push_back(ss.str());

  ss.str("");
  ss << energy;
  energy_vector.push_back(ss.str());

  ss.str("");
  ss << time;
  time_vector.push_back(ss.str());

  ss.str("");
  ss << transmit;
  transmit_vector.push_back(ss.str());

  ss.str("");
  ss << bandwidth;
  bandwidth_vector.push_back(ss.str());

  ss.str("");
  ss << neighbor_energy;
  neighbor_energy_vector.push_back(ss.str());

  ss.str("");
  ss << neighbor_time;
  neighbor_time_vector.push_back(ss.str());

  ss.str("");

  size++;
}


/*****************************************************************************/
void DataSet::getDomain(std::set<int>& domain)
{
  domain.insert(domain_set.begin(), domain_set.end());
}

void DataSet::getFrequencyDomain(std::set<int>& domain)
{
  domain.insert(frequency_domain_set.begin(), frequency_domain_set.end());
}

int DataSet::getTotalReward()
{
  return total_reward;
}

int DataSet::getTotalEnergy()
{
  return total_energy;
}

int DataSet::getTotalTime()
{
  return total_time;
}

int DataSet::getTotalBandwidth()
{
  return total_bandwidth;
}

int DataSet::getTotalneighborEnergy()
{
  return total_neighbor_energy;
}

int DataSet::getTotalneighborTime()
{
  return total_neighbor_time;
}

void DataSet::getSet(std::string& domain_string,
                      std::string& reward_string,
                      std::string& energy_string,
                      std::string& time_string,
                      std::string& transmit_string,
                      std::string& bandwidth_string,
                      std::string& neighbor_energy_string,
                      std::string& neighbor_time_string,
                      std::string& frequency_string)
{
  if (domain_vector.empty())
    return;

  domain_string.assign(domain_vector[0]);
  reward_string.assign(reward_vector[0]);
  energy_string.assign(energy_vector[0]);
  time_string.assign(time_vector[0]);
  transmit_string.assign(transmit_vector[0]);
  bandwidth_string.assign(bandwidth_vector[0]);
  neighbor_energy_string.assign(neighbor_energy_vector[0]);
  neighbor_time_string.assign(neighbor_time_vector[0]);

  for (size_t i = 1; i < domain_vector.size(); i++)
  {
    domain_string.append(", ");
    domain_string.append(domain_vector[i]);

    reward_string.append(", ");
    reward_string.append(reward_vector[i]);

    energy_string.append(", ");
    energy_string.append(energy_vector[i]);

    time_string.append(", ");
    time_string.append(time_vector[i]);

    transmit_string.append(", ");
    transmit_string.append(transmit_vector[i]);

    bandwidth_string.append(", ");
    bandwidth_string.append(bandwidth_vector[i]);

    neighbor_energy_string.append(", ");
    neighbor_energy_string.append(neighbor_energy_vector[i]);

    neighbor_time_string.append(", ");
    neighbor_time_string.append(neighbor_time_vector[i]);
  }


  std::set<int>::iterator frequency_domain_set_it = frequency_domain_set.begin();
  std::stringstream ss;
  ss << "{ " << *frequency_domain_set_it;

  for ( frequency_domain_set_it++;
        frequency_domain_set_it != frequency_domain_set.end();
        frequency_domain_set_it++)
  {
    ss << ", " << *frequency_domain_set_it;
  }
  ss << " }";
  frequency_string.assign(ss.str());


}

bool DataSet::empty()
{
  if (domain_vector.empty())
    return true;
  else
    return false;
}

int DataSet::getSize()
{
  return size;
}


/*****************************************************************************/

std::string DataSet::toString()
{
  std::string output;

  std::string domain_string;
  std::string reward_string;
  std::string energy_string;
  std::string time_string;
  std::string transmit_string;
  std::string bandwidth_string;
  std::string neighbor_energy_string;
  std::string neighbor_time_string;
  std::string frequency_string;

  DataSet::getSet(domain_string, reward_string, energy_string, time_string, transmit_string, bandwidth_string, neighbor_energy_string, neighbor_time_string, frequency_string);


  output.append(domain_string);
  output.append("\n");
  output.append(reward_string);
  output.append("\n");
  output.append(energy_string);
  output.append("\n");
  output.append(time_string);
  output.append("\n");
  output.append(transmit_string);
  output.append("\n");
  output.append(bandwidth_string);
  output.append("\n");
  output.append(neighbor_energy_string);
  output.append("\n");
  output.append(neighbor_time_string);
  output.append("\n");
  output.append(frequency_string);
  output.append("\n");

  return output;
}





